<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/22
 * Time: 9:16
 */

namespace app\api\controller;


use think\Db;

class My extends Base
{
    /**
     * 完善个人信息
     * @return \think\response\Json
     * user_image  头像
     * user_sheng  省ID
     * user_shi  市ID
     * user_qu  区ID
     * user_school  学校
     * user_name  姓名
     * user_gender  性别  1 男  2 女
     * user_student  学籍号   角色为学生时传
     * user_grade  年级   角色为学生时传
     * user_class  班级    角色为学生时传
     * teacher_grade_class  年级&班级组成的二维数组   角色为老师时传
     * 结构{{'user_grade'=> 1, 'user_class'=> 1},{'user_grade'=> 1, user_class=> 2}}
     */
    public function completeInfo()
    {
        $params = input('param.');
        $send_book = '';
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $data = [];
        $grade_class = [];
        $userInfo = Db::name('u_user')->where('user_id', $params['user_id'])->find();

        Db::startTrans();
        try {
            //头像
            if($params['user_image'] != '') {
                $data['user_image'] = $params['user_image'];
            }
            //省市区
            if($params['user_sheng'] != '' && $params['user_shi'] != '' && $params['user_qu'] != '') {
                $data['user_sheng'] = $params['user_sheng'];
                $data['user_shi'] = $params['user_shi'];
                $data['user_qu'] = $params['user_qu'];
                $data['user_ssq_str'] = $this->areaStr($params['user_sheng'], $params['user_shi'], $params['user_qu']);
            }
            //学校
            if($params['user_school'] != '') {
                $data['user_school'] = $params['user_school'];
                $grade_class['gs_school_id'] = $params['user_school'];
                //学校的认证人数+1
                Db::table('up_school')->where('school_id', $params['user_school'])->setInc('school_student_num');
            }
            //姓名
            if($params['user_name'] != '') {
                $data['user_username'] = $params['user_name'];
                $data['user_nickname'] = $params['user_name'];
            }
            //性别  1 男  2 女
            if($params['user_gender'] != '') {
                $data['user_gender'] = $params['user_gender'];
            }
            if($userInfo['user_type'] == 1) {//学生
                //年级
                if($params['user_grade'] != '') {
                    $grade_class['gs_grade_id'] = $params['user_grade'];
                    $data['user_grade'] = $params['user_grade'];
                }
                //班级
                if($params['user_class'] != '') {
                    $grade_class['gs_class_id'] = $params['user_class'];
                    $data['user_class'] = $params['user_class'];
                }
                //学籍号
                if($params['user_student'] != '') {
                    $data['user_student'] = $params['user_student'];
                }
                $grade_class['gs_user_id'] = $params['user_id'];
                $grade_class['gs_role'] = 1;
                //更新关联表
                Db::table('up_grade_class')
                    ->where('gs_user_id', $params['user_id'])
                    ->update($grade_class);
            } else {
                $teacher_grade_class = input('param.teacher_grade_class');
                $teacher_grade_class = json_decode($teacher_grade_class, true);

                if($teacher_grade_class != '') {
                    foreach($teacher_grade_class as $k => $v) {
                        $grade_class_arr['gs_user_id'] = $params['user_id'];
                        $grade_class_arr['gs_grade_id'] = $v['user_grade'];
                        $grade_class_arr['gs_class_id'] = $v['user_class'];
                        $grade_class_arr['gs_role'] = 2;
                        $grade_class_arr['gs_school_id'] = $v['gs_school_id'];
                        //插入关联表
                        Db::table('up_grade_class')->insert($grade_class_arr);
                    }
                }
                
                //*************  完善个人信息不需要身份证验证
//                $data['user_card_front'] = $params['user_card_front']; //身份证正面
//                $data['user_card_back'] = $params['user_card_back']; //身份证反面
//                $data['user_zige'] = $params['user_zige']; //资格证书
            }

            $data['user_update_time'] = date('Y-m-d H:i:s');
            //更新用户表
            Db::table('up_u_user')->where('user_id', $params['user_id'])->update($data);
            //完善资料送书
            $info = Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->find();
            if($info['gs_school_id'] != '' && $info['gs_grade_id'] != '' && $info['gs_class_id'] != '') {
                $res_send = $this->wanshanSendBook($params['user_id']);
                if($res_send){
                    $send_book = $res_send;
                }
            }
            Db::commit();
            $res = 1;
        } catch(\Exception $e) {
            $r = $e->getMessage();
            Db::rollback();
            $res = 0;
        }

        if($res) {
            $user_info = Db::table('up_u_user')->where('user_id', $params['user_id'])->find();
            $rytoken = $this->rongyun($user_info['user_id'],'who',$user_info['user_image']);
            $user_info['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $user_info, 'send_book' => $send_book]);
        } else {
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 获取省市区的三维数组
     * @return \think\response\Json
     */
    public function getArea()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $provinceLists = Db::name('area')->where('level', 1)->select();//省
        $cityLists = Db::name('area')->where('level', 2)->select();//市
        $districtLists = Db::name('area')->where('level', 3)->select();//区
        foreach($cityLists as $k => $city) {
            foreach($districtLists as $kk => $district) {
                if($city['district_id'] == $district['pid']) {
                    $cityLists[$k]['qu'][] = $district;
                }
            }
        }
        foreach($provinceLists as $key => $province) {
            foreach($cityLists as $ke => $city) {
                if($province['district_id'] == $city['pid']) {
                    $provinceLists[$key]['city'][] = $city;
                }
            }
        }
        return json(['status' => 200, 'msg' => $provinceLists]);
        
    }
    
    /**
     * 根据所选区ID筛选所有学校
     * @return \think\response\Json
     * qu_id   区ID
     * key     学校名称
     */
    public function getSchoolByQu()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $where = [
            'school_status' => 1,
            'school_del' => 1,
            'school_qu' => $params['qu_id']
        ];
        if(isset($params['key']) && $params['key'] != ''){
            $where['school_name'] = ['like', '%' . $params['key'] . '%'];
        }
        $school = Db::name('school')
            ->field('school_id, school_name')
            ->where($where)
            ->order('school_time desc, school_id desc')
            ->select();
        if($school) {
            return json(['status' => 200, 'msg' => $school]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 选择年级
     * @return \think\response\Json
     * school_id  学校ID
     */
    public function getGradeBySchool()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $grade = Db::name('relevance')
            ->join('up_grade', 'grade_id = re_grade_id', 'left')
            ->field('re_grade_id, grade_name')
            ->where('grade_del', 1)
            ->where('grade_status', 1)
            ->where('re_school_id', $params['school_id'])
            ->group('re_grade_id')
            ->order('grade_sort')
            ->select();
        if($grade) {
            return json(['status' => 200, 'msg' => $grade]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 选择班级
     * @return \think\response\Json
     * school_id  学校ID
     * grade_id  年级ID
     */
    public function getClassByGrade()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $class = Db::name('relevance')
            ->join('up_class', 'class_id = re_class_id', 'left')
            ->field('re_class_id, class_name')
            ->where('class_del', 1)
            ->where('class_status', 1)
            ->where('re_school_id', $params['school_id'])
            ->where('re_grade_id', $params['grade_id'])
            ->group('re_class_id')
            ->order('class_sort desc')
            ->select();
        if($class) {
            return json(['status' => 200, 'msg' => $class]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 编辑头像
     * @return \think\response\Json
     * image  用户头像
     */
    public function changeImage()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $res = Db::name('u_user')
            ->where('user_id', $params['user_id'])
            ->update(['user_image' => $params['image']]);
        if($res) {
            return json(['status' => 200, 'msg' => '修改成功']);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑用户名
     * @return \think\response\Json
     * name  用户头像
     */
    public function changeName()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $res = Db::name('u_user')
            ->where('user_id', $params['user_id'])
            ->update(['user_username' => $params['name']]);
        if($res) {
            return json(['status' => 200, 'msg' => '修改成功']);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑性别
     * @return \think\response\Json
     * gender  性别   1 男   2 女
     */
    public function changeGender()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $res = Db::name('u_user')
            ->where('user_id', $params['user_id'])
            ->update(['user_gender' => $params['gender']]);
        if($res) {
            return json(['status' => 200, 'msg' => '修改成功']);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑姓名
     * @return \think\response\Json
     * user_name  姓名
     */
    public function changeUsername()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $res = Db::name('u_user')
            ->where('user_id', $params['user_id'])
            ->update(['user_username' => $params['user_name'], 'user_nickname' => $params['user_name']]);
        if($res) {
            return json(['status' => 200, 'msg' => '修改成功']);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑生日
     * @return \think\response\Json
     * birthday  生日
     */
    public function changeBirthday()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $time = strtotime($params['birthday']);//生日的时间戳
        $year = date('Y', $time);//生日----年
        $month = date('m', $time);//生日----月
        $day = date('d', $time);//生日----日
        //当前年月日
        $nyear = date('Y');
        $nmonth = date('m');
        $nday = date('d');
        //计算年龄
        $age = $nyear - $year;
        //如果当前时间的month小于生日的month，则年龄-1岁
        if($nmonth < $month) {
            $age = $age - 1;
            //如果当前时间的month等于生日的month，并且当前时间的day小于生日的day，则年龄-1
        } elseif($nmonth == $month && $nday < $day) {
            $age = $age - 1;
        }
        $data = [
            'user_birthday' => $year . '年' . $month . '月' . $day . '日',
            'user_age' => $age
        ];
        $res = Db::name('u_user')
            ->where('user_id', $params['user_id'])
            ->update($data);
        if($res) {
            return json(['status' => 200, 'msg' => $data['user_birthday']]);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑学籍号
     * @return \think\response\Json
     * user_student  学籍号
     */
    public function changeUserStudent()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $res = Db::name('u_user')
            ->where('user_id', $params['user_id'])
            ->update(['user_student' => $params['user_student']]);
        if($res) {
            return json(['status' => 200, 'msg' => $params['user_student']]);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑地区
     * @return \think\response\Json
     * user_sheng  省ID
     * user_shi  市ID
     * user_qu  区ID
     */
    public function changeArea()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        //如果为老师，则需要先清空关联表记录
        $user_type = Db::table('up_u_user')->where('user_id', $params['user_id'])->value('user_type');
        if($user_type == 2){
            Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->delete();
        }
        $data = [
            'user_sheng' => $params['user_sheng'],
            'user_shi' => $params['user_shi'],
            'user_qu' => $params['user_qu'],
            'user_ssq_str' => $this->areaStr($params['user_sheng'], $params['user_shi'], $params['user_qu']),
        ];
        $res = Db::name('u_user')
            ->where('user_id', $params['user_id'])
            ->update($data);
        if($res){
            return json(['status' => 200, 'msg' => '修改成功']);
        }else{
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑学校
     * @return \think\response\Json
     * old_school_id  原学校ID  没有则传空
     * school_id      新学校ID
     */
    public function changeSchool()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $userGrade = Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->find();
        $userInfo = Db::table('up_u_user')->where('user_id', $params['user_id'])->find();
        //如果为老师，则需要先清空关联表记录
        if($userInfo['user_type'] == 2){
            Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->delete();
        }
        Db::startTrans();
        try {
            //---------------更新用户表学校数据---------
            Db::name('u_user')
                ->where('user_id', $params['user_id'])
                ->update(['user_school' => $params['school_id']]);
            //------------------有记录就更新
            if($userGrade) {
                //----------------更新关联表数据-----------
                $where['gs_user_id'] = $params['user_id'];
                if($userInfo['user_school'] != '') {
                    $where['gs_school_id'] = $userInfo['user_school'];
                }
                
                Db::table('up_grade_class')->where($where)->update(['gs_school_id' => $params['school_id']]);
                //-----------------更新学校认证人数----
                //原学校人数减1
                Db::table('up_school')->where('school_id', $userInfo['user_school'])->setDec('school_student_num');
                //新学校人数加1
                Db::table('up_school')->where('school_id', $params['school_id'])->setInc('school_student_num');
            } else {
                //----------------没有记录就插入
                if($userInfo['user_type'] == 1){
                    $data = [
                        'gs_user_id' => $params['user_id'],
                        'gs_school_id' => $params['school_id'],
                        'gs_role' => 1
                    ];
                    Db::name('grade_class')->insert($data);
                }
                //新学校人数加1
                Db::table('up_school')->where('school_id', $params['school_id'])->setInc('school_student_num');
            }
            
            Db::commit();
            $result = 1;
        } catch(\Exception $e) {
            Db::rollback();
            $result = 0;
        }
        
        if($result == 1) {
            return json(['status' => 200, 'msg' => '修改成功']);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑年级----学生
     * @return \think\response\Json
     * old_grade_id   原班级ID  没有则传空
     * grade_id       新年级ID
     */
    public function changeGrade()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $userGrade = Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->find();
        //--------------------有记录就修改--------
        Db::startTrans();
        try {
            if($userGrade) {
                $where['gs_user_id'] = $params['user_id'];
                if($params['old_grade_id'] != '') {
                    $where['gs_grade_id'] = $params['old_grade_id'];
                }
                
                Db::name('grade_class')->where($where)->update(['gs_grade_id' => $params['grade_id']]);
            } else {
                //---------------没记录就插入-------
                $data = [
                    'gs_user_id' => $params['user_id'],
                    'gs_grade_id' => $params['grade_id']
                ];
                Db::name('grade_class')->insert($data);
            }
            //更新用户表
            Db::table('up_u_user')->where('user_id', $params['user_id'])->update(['user_grade' => $params['grade_id']]);
            Db::commit();
            $result = 1;
        } catch(\Exception $e) {
            Db::rollback();
            $result = 0;
        }
        
        if($result == 1) {
            return json(['status' => 200, 'msg' => '修改成功']);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 编辑班级----学生
     * @return \think\response\Json
     * old_class_id   原班级ID  没有就传空
     * class_id       新班级ID
     */
    public function changeClass()
    {
        $send_book = '';
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $userGrade = Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->find();
        //---------------------有记录就修改----------------
        Db::startTrans();
        try {
            if($userGrade) {
                $where['gs_user_id'] = $params['user_id'];
                if($params['old_class_id'] != '') {
                    $where['gs_class_id'] = $params['old_class_id'];
                }
                
                Db::name('grade_class')->where($where)->update(['gs_class_id' => $params['class_id']]);
            } else {
                //-------------------没记录就插入------------
                $data = [
                    'gs_user_id' => $params['user_id'],
                    'gs_class_id' => $params['class_id']
                ];
                Db::name('grade_class')->insert($data);
            }
            //更新用户表
            Db::table('up_u_user')->where('user_id', $params['user_id'])->update(['user_class' => $params['class_id']]);
            $res_send = $this->wanshanSendBook($params['user_id']);
            if($res_send){
                $send_book = $res_send;
            }
            Db::commit();
            $result = 1;
        } catch(\Exception $e) {
            Db::rollback();
            $result = 0;
        }
        
        if($result == 1) {
            return json(['status' => 200, 'msg' => '修改成功', 'send_book' => $send_book]);
        } else {
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 老师修改年级-班级
     * @return \think\response\Json
     * type   操作类型  1 删除   2 添加
     *        gs_school_id  学校ID   type为2时
     *        gs_grade_id   年级ID   type为2时
     *        gs_classs_id  班级ID   type为2时
     *        gs_id         记录ID   type为1时
     */
    public function decGradeClass(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        if($params['type'] == 1){//删除
            $res = Db::table('up_grade_class')->where('gs_id', $params['gs_id'])->delete();
        }else{//添加
            $data = [
                'gs_user_id' => $params['user_id'],
                'gs_school_id' => $params['gs_school_id'],
                'gs_grade_id' => $params['gs_grade_id'],
                'gs_class_id' => $params['gs_class_id'],
                'gs_role' => 2
            ];
            $res = Db::table('up_grade_class')->insert($data);
        }
        if($res){
            $send_book = '';
            $res_send = $this->wanshanSendBook($params['user_id']);
            if($res_send){
                $send_book = $res_send;
            }
            return json(['status' => 200, 'msg' => '操作成功', 'send_book' => $send_book]);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 验证地区和学校是否相符
     * @return \think\response\Json
     * school_id  学校ID
     * qu_id   地区ID
     */
    public function verifySchool()
    {
        $params = input('param.');
        $qu = Db::table('up_school')->where('school_id', $params['school_id'])->value('school_qu');
        if($qu != $params['qu_id']) {
            return json(['status' => 0, 'msg' => '地区与学校不符，请重新选择地区或学校']);
        } else {
            return json(['status' => 200, 'msg' => true]);
        }
    }
    
    /**
     * 验证年级和学校是否相符
     * @return \think\response\Json
     * school_id  学校ID
     * grade_id   年级ID
     */
    public function verifyGrade()
    {
        $params = input('param.');
        $school = Db::table('up_relevance')
            ->where('re_grade_id', $params['school_id'])
            ->where('re_grade_id', $params['grade_id'])
            ->find();
        if($school) {
            return json(['status' => 0, 'msg' => '学校与班级不符，请重新选择班级或学校']);
        } else {
            return json(['status' => 200, 'msg' => true]);
        }
    }
    
    /**
     * 验证年级和班级是否相符
     * @return \think\response\Json
     * school_id  学校ID
     * grade_id   年级ID
     * class_id   班级ID
     */
    public function verifyClass()
    {
        $params = input('param.');
        $grade = Db::table('up_relevance')
            ->where('re_school_id', $params['school_id'])
            ->where('re_grade_id', $params['grade_id'])
            ->where('re_class_id', $params['class_id'])
            ->find();
        if(!$grade) {
            return json(['status' => 0, 'msg' => '年级与班级不符，请重新选择班级或年级']);
        } else {
            return json(['status' => 200, 'msg' => true]);
        }
    }
    
    /**
     * 用户基本信息
     * @return \think\response\Json
     */
    public function userInfo()
    {
        extract(input());
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        //角色
        $user_type = Db::table('up_u_user')->where('user_id', $params['user_id'])->value('user_type');
        if($user_type == 1){//学生
            $userInfo = Db::table('up_u_user')
//                ->join('up_grade_class', 'gs_user_id = user_id', 'left')
                ->join('up_grade', 'grade_id = user_grade', 'left')
                ->join('up_class', 'class_id = user_class', 'left')
                ->join('up_area', 'user_qu = district_id', 'left')
                ->join('up_school', 'school_id = user_school', 'left')
                ->field('user_image, user_gender, user_age, grade_name, class_name, district, 
            school_name, user_username,user_exp,user_integral, user_status')
                ->where('user_id', $params['user_id'])
                ->find();
        }else{
            $userInfo = Db::table('up_u_user')
                ->join('up_grade_class', 'gs_user_id = user_id', 'left')
                ->join('up_grade', 'grade_id = gs_grade_id', 'left')
                ->join('up_class', 'class_id = gs_class_id', 'left')
                ->join('up_area', 'user_qu = district_id', 'left')
                ->join('up_school', 'school_id = user_school', 'left')
                ->field('user_image, user_gender, user_age, grade_name, class_name, district, 
            school_name, user_username,user_exp,user_integral, user_status')
                ->where('user_id', $params['user_id'])
                ->find();
        }
        
        //学校
        if($userInfo['district'] && $userInfo['school_name']) {
            $userInfo['address'] = $userInfo['district'] . $userInfo['school_name'];
        } else {
            $userInfo['address'] = null;
        }
        //年级班级
        if($userInfo['grade_name'] && $userInfo['class_name']) {
            $userInfo['classroom'] = $userInfo['grade_name'] . $userInfo['class_name'];
        } else {
            $userInfo['classroom'] = null;
        }
        //只有年级
        if($userInfo['grade_name'] != '' && $userInfo['class_name'] == ''){
            $userInfo['classroom'] = $userInfo['grade_name'];
        }
        if($userInfo) {
            $userInfo['level'] = $this->getUserExpLevel($params['user_id']);//段位
            $userInfo['non_payment'] = $this->order($params['user_id'], 1, 1, 2);//未支付数量
            $userInfo['non_send'] = $this->order($params['user_id'], 2, 1, 2);//待发货数量
            $userInfo['non_receiving'] = $this->order($params['user_id'], 3, 1, 2);//待收货数量
            $userInfo['aftersale'] = $this->afterSale($params['user_id'], 1, 2); //售后数量
            $unused = $this->couponList($params['user_id'], 1, 2);
            $used = $this->couponList($params['user_id'], 2, 2);
            $past = $this->couponList($params['user_id'], 3, 2);
//            $userInfo['num_coupon'] = $unused + $used + $past;//优惠券总数量
            $userInfo['num_coupon'] = $unused;//优惠券总数量

            $mapWhere['friend_status'] = 1;
            $mapWhere['friend_user_id'] = $user_id;
            $list = Db::table('up_friend')->where($mapWhere)->count();

            $count_wait = Db::table('up_friend')
                ->where('friend_other_user',$user_id)
                ->where('friend_status',2)
                ->count();
            return json(['status' => 200, 'msg' => $userInfo,'num'=>$list,'count_wait'=>$count_wait]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 用户详细信息
     * @return \think\response\Json
     */
    public function userDetail()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $user_type = Db::table('up_u_user')->where('user_id', $params['user_id'])->value('user_type');
        if($user_type == 1){//学生
            $userDetail = Db::table('up_u_user')
//                ->join('up_grade_class', 'gs_user_id = user_id', 'left')
                ->join('up_grade', 'grade_id = user_grade', 'left')
                ->join('up_class', 'class_id = user_class', 'left')
                ->join('up_area ua', 'user_sheng = ua.district_id', 'left')
                ->join('up_area ua1', 'user_shi = ua1.district_id', 'left')
                ->join('up_area ua2', 'user_qu = ua2.district_id', 'left')
                ->join('up_school', 'school_id = user_school', 'left')
                ->field('user_image, user_username, user_gender, user_birthday, grade_name, grade_id, 
            class_name, class_id, school_name, school_id, ua.district_id as province_id, 
            ua.district as province_name, ua1.district_id as city_id, ua1.district as city_name, 
            ua2.district_id, ua2.district as district_name, user_student, user_card_front, user_card_back, 
            user_status, user_zige')
                ->where('user_id', $params['user_id'])
                ->find();
        }else{//老师
            //年级、班级数组
            $sun = Db::table('up_grade_class')
                ->join('up_grade', 'grade_id = gs_grade_id', 'left')
                ->join('up_class', 'class_id = gs_class_id', 'left')
                ->where('gs_user_id', $params['user_id'])
                ->field('gs_id, class_name, grade_name')
                ->group('gs_grade_id, gs_class_id')
                ->select();
            
            $userDetail = Db::table('up_u_user')
                ->join('up_area ua', 'user_sheng = ua.district_id', 'left')
                ->join('up_area ua1', 'user_shi = ua1.district_id', 'left')
                ->join('up_area ua2', 'user_qu = ua2.district_id', 'left')
                ->join('up_school', 'school_id = user_school', 'left')
                ->field('user_image, user_username, user_gender, user_birthday, 
            school_name, school_id, ua.district_id as province_id, ua.district as province_name, 
            ua1.district_id as city_id, ua1.district as city_name, ua2.district_id, ua2.district as district_name, 
            user_card_front, user_card_back, user_status')
                ->where('user_id', $params['user_id'])
                ->find();
            $userDetail['sun'] = $sun;
        }
        
        if($userDetail) {
            return json(['status' => 200, 'msg' => $userDetail]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 用户收货地址列表
     * @return \think\response\Json
     */
    public function myAddress()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $where = array();
        $where['address_user_id'] = $params['user_id'];
        $where['address_del'] = 1;
        if(!isset($fromset)){
            $where['ua1.status'] = 1;
        }
        $address = Db::table('up_my_address')
            ->join('up_area ua', 'address_sheng = ua.district_id', 'left')
            ->join('up_area ua1', 'address_shi = ua1.district_id', 'left')
            ->join('up_area ua2', 'address_qu = ua2.district_id', 'left')
            ->field('address_id, address_name, address_phone, ua.district_id as province_id, 
            ua.district as province_name, ua1.district_id as city_id, ua1.district as city_name, 
            ua2.district_id, ua2.district as district_name, address_detail, is_default')
            ->where($where)
            ->order('address_time desc')
            ->page($params['page'], 10)
            ->select();
        if($address) {
            return json(['status' => 200, 'msg' => $address]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 添加收货地址
     * @return \think\response\Json
     * address_name  收货人
     * address_phone  收货电话
     * address_sheng  省ID
     * address_shi  市ID
     * address_qu  区ID
     * address_detail  详细地址
     * is_default  是否默认  1 不是   2 是
     */
    public function addMyAddress()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        //判断所选城市是否可配送
        $is_pei = Db::table('up_area')->where('district_id', $params['address_shi'])->value('status');
        if($is_pei == 2) return json(['status' => 100, 'msg' => '您设置的地区不在当前支持的配送范围内']);
        
        $data = [
            'address_name' => $params['address_name'],//收货人
            'address_phone' => $params['address_phone'],//收货人电话
            'address_sheng' => $params['address_sheng'],//省
            'address_shi' => $params['address_shi'],//市
            'address_qu' => $params['address_qu'],//区
            'address_detail' => $params['address_detail'],//详细地址
            'address_time' => date('Y-m-d H:i:s'),//添加时间
            'address_update_time' => date('Y-m-d H:i:s'),//编辑时间
            'is_default' => $params['is_default'],//是否默认   1 不默认   2 默认
            'address_user_id' => $params['user_id'],
            'address_telephone' => $params['address_phone']
        ];
        Db::startTrans();
        try {
            if($params['is_default'] == 2) {//如果添加的时候设置为默认地址，
                Db::table('up_my_address')->where('address_user_id', $params['user_id'])->setField('is_default', 1);
            }
            Db::table('up_my_address')->insert($data);
            Db::commit();
            $res = true;
        } catch(\Exception $e) {
            Db::rollback();
            $res = false;
        }
        if($res) {
            return json(['status' => 200, 'msg' => '添加成功']);
        } else {
            return json(['status' => 0, 'msg' => '添加失败']);
        }
    }
    
    /**
     * 收货地址详情
     * @return \think\response\Json
     * address_id  收货地址ID
     */
    public function addressInfo()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $addressInfo = Db::table('up_my_address')
            ->join('__AREA__ ua', 'address_sheng = ua.district_id', 'left')
            ->join('__AREA__ ua1', 'address_shi = ua1.district_id')
            ->join('__AREA__ ua2', 'address_qu = ua2.district_id')
            ->field('address_id, address_name, address_phone, address_detail, is_default, 
            ua.district_id as province_id, ua.district as province_name, ua1.district_id as city_id, 
            ua1.district as city_name, ua2.district_id, ua2.district as district_name,address_telephone')
            ->where('address_id', $params['address_id'])
            ->find();
        if($addressInfo) {
            return json(['status' => 200, 'msg' => $addressInfo]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
        
    }
    
    /**
     * 编辑收货地址
     * @return \think\response\Json
     * address_id  收货地址ID
     */
    public function editMyAddress()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        //判断所选城市是否可配送
        $is_pei = Db::table('up_area')->where('district_id', $params['address_shi'])->value('status');
        if($is_pei == 2) return json(['status' => 100, 'msg' => '您设置的地区不在当前支持的配送范围内']);
        
        $data = [
            'address_name' => $params['address_name'],//收货人
            'address_phone' => $params['address_phone'],//收货人电话
            'address_sheng' => $params['address_sheng'],//省
            'address_shi' => $params['address_shi'],//市
            'address_qu' => $params['address_qu'],//区
            'address_detail' => $params['address_detail'],//详细地址
            'address_update_time' => date('Y-m-d H:i:s'),//编辑时间
            'is_default' => $params['is_default'],//是否默认   1 不默认   2 默认
            'address_telephone' => $params['address_phone']
        ];
        Db::startTrans();
        try {
            if($params['is_default'] == 2) {//如果编辑的时候设置为默认地址，
                Db::table('up_my_address')->where('address_user_id', $params['user_id'])->setField('is_default', 1);
            }
            Db::table('up_my_address')->where('address_id', $params['address_id'])->update($data);
            Db::commit();
            $result = 1;
        } catch(\Exception $e) {
            Db::rollback();
            $result = 0;
        }
        if($result) {
            return json(['status' => 200, 'msg' => '编辑成功']);
        } else {
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 设置默认收货地址
     * @return \think\response\Json
     * address_id   收货地址ID
     */
    public function setDefaultAddress()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $detault = Db::table('up_my_address')->where('address_id', $params['address_id'])->value('is_default');
        Db::startTrans();
        try {
            if($detault == 1) {
                //将所有默认地址都改为非默认
                Db::table('up_my_address')->where('address_user_id', $params['user_id'])->setField('is_default', 1);
                //设置默认地址
                Db::table('up_my_address')->where('address_id', $params['address_id'])->setField('is_default', 2);
            }
            Db::commit();
            $res = true;
        } catch(\Exception $e) {
            Db::rollback();
            $res = false;
        }
        if($res) {
            return json(['status' => 200, 'msg' => '设置成功']);
        } else {
            return json(['status' => 0, 'msg' => '设置失败']);
        }
    }
    
    /**
     * 删除默认收货地址
     * @return \think\response\Json
     * address_id   收货地址ID
     */
    public function delMyAddress(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $res = Db::table('up_my_address')->where('address_id', $params['address_id'])->setField('address_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 用户协议
     * @return \think\response\Json
     */
    public function aboutUs()
    {
        $content = Db::table('up_about')->where('id', 1)->value('content');
        return json(['status' => 200, 'msg' => $content]);
    }
    
    /**
     * 常见问题列表
     * @return \think\response\Json
     * page  页码
     */
    public function famQuestionList()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $list = Db::table('up_familiar_question')
            ->field('fa_id, fa_title, fa_content, fa_time')
            ->order('fa_time desc')
            ->page($params['page'], 10)
            ->select();
        if($list) {
            return json(['status' => 200, 'msg' => $list]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 常见问题详情
     * @return \think\response\Json
     * fa_id  问题ID
     */
    public function famQuestionDetail()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $detail = Db::table('up_familiar_question')
            ->field('fa_id, fa_title, fa_content, fa_time')
            ->where('fa_id', $params['fa_id'])
            ->find();
        if($detail) {
            return json(['status' => 200, 'msg' => $detail]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 问题反馈
     * @return \think\response\Json
     * feedback_content  反馈内容
     */
    public function feedback()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $data = [
            'feedback_content' => $params['feedback_content'],
            'feedback_user_id' => $params['user_id'],
            'feedback_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_feedback')->insert($data);
        if($res) {
            return json(['status' => 200, 'msg' => '反馈成功']);
        } else {
            return json(['status' => 0, 'msg' => '反馈失败']);
        }
    }
    
    /**
     * 我的优惠券列表/数量
     * @return \think\response\Json
     * page  页码
     * status  优惠券状态  1 未使用  2 已使用  3 已过期
     */
    public function myCoupon()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $map['order_userid'] = $params['user_id'];
        $map['order_status'] = 5;
        $result = Db::table('up_order')->where($map)->field('order_is_coupon,order_my_coupon')->select();
        foreach ($result as $key=>$vo){
            if($vo['order_is_coupon']==1){
                Db::table('up_my_coupon')->where('mc_id',$vo['order_my_coupon'])->setField('mc_status',1);
            }
        }
        $page = input('page') ? input('page') : 1;
        $this->timeOverCoupon($params['user_id']);
        $status = input('status');//1 未使用  2 已使用  3 已过期
        $list = $this->couponList($params['user_id'], $status, 1, $page);
        $num = [
            'unused_num' => $this->couponList($params['user_id'], 1, 2),//未使用
            'been_use_num' => $this->couponList($params['user_id'], 2, 2),//已使用
            'past_num' => $this->couponList($params['user_id'], 3, 2),//已过期
        ];
        if($list) {
            return json(['status' => 200, 'msg' => $list, 'num' => $num]);
        } else {
            return json(['status' => 200, 'msg' => [], 'num' => $num]);
        }
    }
    
    /**
     * 删除我的优惠券
     * @return \think\response\Json
     * mc_ids  被删除的优惠券id   如果只删除一条则为该条id   如果多条则为id,id的样式
     */
    public function myCouponDel()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $mc_ids = input('mc_ids');
        $res = Db::table('up_my_coupon')->where('mc_id', 'IN', $mc_ids)->delete();
        if($res) {
            return json(['status' => 200, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 错题列表
     * @return \think\response\Json
     */
    public function myErrorBook()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $page = input('page') ? input('page') : 1;
        $errorList = Db::table('up_error_book')
            ->join('up_fight_subject', 'fs_id = er_fs_id', 'right')
            ->field('fs_name, er_id, er_fs_id, er_time')
            ->where('er_user_id', $params['user_id'])
            ->order('er_time desc')
            ->page($page, 20)
            ->select();
        return json(['status' => 200, 'msg' => $errorList]);
    }
    
    /**
     * 错题详情
     * @return \think\response\Json
     * er_id  错题ID
     * er_fs_id  原题ID
     */
    public function errorDetail()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $er_id = input('er_id');//错题ID
//        $fs_id = input('er_fs_id');//题目ID
        $fs_id = Db::table('up_error_book')
            ->where('er_id', $er_id)
            ->value('er_fs_id');
        $is_del = Db::table('up_fight_subject')->where('fs_id', $fs_id)->find();
        //如果原题被删，则删除该条错题记录，并提示原题被删除
        if(empty($is_del)) {
            Db::table('up_error_book')->delete($er_id);
            return json(['status' => 0, 'msg' => '原题目已被删除']);
        }
        $detail = Db::table('up_error_book')
            ->join('up_fight_subject', 'fs_id = er_fs_id', 'left')
            ->field('fs_name, fs_a, fs_b, fs_c, fs_d, fs_answer, er_answer')
            ->where('er_id', $er_id)
            ->find();
        if($detail) {
            return json(['status' => 200, 'msg' => $detail]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除错题
     * @return \think\response\Json
     * er_id  错题ID
     */
    public function errorDel()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $er_id = input('er_id');
        $er_id = trim($er_id,',');
        $res = Db::table('up_error_book')->where('er_id','in',$er_id)->delete();
        if($res) {
            return json(['status' => 200, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 我的班级---学生
     * @return \think\response\Json
     */
    public function studentClass()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $class = Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->find();
        if(empty($class['gs_class_id']) || empty($class['gs_school_id'])){
            //如果该学生被老师从该班级删除或者还没有选学校
            return json(['status' => 200, 'msg' => []]);
        }else{
            $gradeName = Db::table('up_grade')->where('grade_id', $class['gs_grade_id'])->value('grade_name');
            $className = Db::table('up_class')->where('class_id', $class['gs_class_id'])->value('class_name');
            $myClassName = $gradeName . $className;
            $myClass = Db::table('up_grade_class')
                ->join('up_u_user', 'user_id = gs_user_id', 'left')
                ->field('user_image, user_username, user_gender, user_age, gs_user_id')
                ->where('gs_grade_id', $class['gs_grade_id'])
                ->where('gs_class_id', $class['gs_class_id'])
                ->where('gs_school_id', $class['gs_school_id'])
                ->where('gs_role', 1)
//            ->where('gs_user_id', 'neq', $params['user_id'])
                ->select();
            if($myClass) {
                foreach($myClass as $key => $val) {
                    $level = $this->getUserExpLevel($val['gs_user_id']);
                    $myClass[$key]['level'] = $level['el_name'];
                }
                return json(['status' => 200, 'msg' => $myClass, 'class' => $myClassName]);
            } else {
                return json(['status' => 200, 'msg' => [], 'class' => $myClassName]);
            }
        }
    }
    
    /**
     * 我的班级----老师
     * @return \think\response\Json
     * time_type  筛选条件   1 周   2 月   3 年
     */
    public function teacherClass()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
    
        $seesee = $this->getUserStatusForSee($params['user_id']);
        if($seesee['status'] != 200){
            return json($seesee);
        }
        
        $time_type = input('time_type');
        if($time_type == 1) {
            $order = 'user_exp_week desc';
        } elseif($time_type == 2) {
            $order = 'user_exp_month desc';
        } else {
            $order = 'user_exp_year desc';
        }

        $class = Db::table('up_grade_class')
            ->join('up_grade', 'grade_id = gs_grade_id', 'left')
            ->join('up_class', 'class_id = gs_class_id', 'left')
            ->field('up_grade_class.*, grade_name, class_name')
            ->where('gs_user_id', $params['user_id'])
            ->group('gs_grade_id, gs_class_id')
            ->select();
        
        foreach($class as $key => $val) {
            //班级名称
            $class_name = $val['grade_name'] . $val['class_name'];
            $class[$key]['class_name'] = $class_name;
            unset($class[$key]['grade_name']);
            
            //学生信息
            $student = Db::table('up_grade_class')
                ->join('up_u_user', 'user_id = gs_user_id', 'left')
                ->field('user_id, user_username, user_image, user_age, user_gender')
                ->where('gs_school_id', $val['gs_school_id'])
                ->where('gs_class_id', $val['gs_class_id'])
                ->where('gs_grade_id', $val['gs_grade_id'])
                ->where('gs_role', 1)
                ->order($order)
                ->select();
            $class[$key]['class_student'] = $student;
            
            foreach($class[$key]['class_student'] as $k => $v) {
                //段位
                $level = $this->getUserExpLevel($v['user_id']);
                $class[$key]['class_student'][$k]['level'] = $level['el_name'];
                
                //班级排名
                $class[$key]['class_student'][$k]['class_sort'] = $k + 1;
                
                //学校排名
                $user_school = Db::table('up_u_user')->where('user_id', $v['user_id'])->value('user_school');
                $school_student = Db::table('up_u_user')
                    ->field('user_id')
                    ->where('user_school', $user_school)
                    ->where('user_status', 1)
                    ->where('user_type', 1)
                    ->order($order)
                    ->select();
                foreach($school_student as $kk => $vv) {
                    if($v['user_id'] == $vv['user_id']) {
                        $class[$key]['class_student'][$k]['school_sort'] = $kk + 1;
                    }
                }
                
                //全国排名
                $country = Db::table('up_u_user')
                    ->field('user_id')
                    ->where('user_status', 1)
                    ->where('user_type', 1)
                    ->order($order)
                    ->select();
                foreach($country as $kkk => $vvv) {
                    if($v['user_id'] == $vvv['user_id']) {
                        $class[$key]['class_student'][$k]['country_sort'] = $kkk + 1;
                    }
                }
            }
        }
        if($class) {
            return json(['status' => 200, 'msg' => $class]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 把学生从本班中删除
     * @return \think\response\Json
     */
    public function delMyStudent(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        Db::startTrans();
        try{
            //关联表
            Db::table('up_grade_class')->where('gs_user_id', 'IN', $params['student_id'])->setField('gs_class_id', null);
            //用户表
            Db::table('up_u_user')->where('user_id', 'IN', $params['student_id'])->setField('user_class', null);
            Db::commit();
            $res = 1;
        }catch(\Exception $e){
            Db::rollback();
            $res = 0;
        }
        if($res){
            return json(['status' => 200, 'msg' => '']);
        }else{
            return json(['status' => 0, 'msg' => '']);
        }
    }
    
    /**
     * 我的订单
     * @return \think\response\Json
     * order_status   订单状态   0 全部   1 待支付   2 代发货   3 待收货
     * order_type     订单类型   1 未申请售后的订单  2 售后订单
     */
    public function myOrder()
    {
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $page = input('page') ? input('page') : 1;
        
        $this->timeOverOrder($params['user_id'], 1);//改变已超时订单状态
        $this->autoVerifyOrder($params['user_id'], 1);//改变15天未收货订单状态
        if($params['order_type'] == 1){
            $order_status = input('order_status');//状态  0 全部  1 待支付  2 代发货  3 待收货
            $orderList = $this->order($params['user_id'], $order_status, $page, 1);
        }else{
            $orderList = $this->afterSale($params['user_id'], $page); //售后列表
        }
        
        if($orderList) {
            if($params['order_type'] == 1){
                foreach($orderList as $k => $v) {
                    $goods = Db::table('up_order_info')
                        ->join('up_goods', 'oi_good_id = good_id', 'left')
                        ->field('oi_id, oi_good_thumb, oi_price, oi_good_coding, oi_good_name, 
                    oi_num, good_type, oi_after_status, after_status')
                        ->where('oi_order_id', $v['order_id'])
                        ->select();
                    if($goods) {
                        $orderList[$k]['goods'] = $goods;
                    } else {
                        $orderList[$k]['goods'] = [];
                    }
                }
            }
            return json(['status' => 200, 'msg' => $orderList]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 订单操作
     * @return \think\response\Json
     * order_id  订单ID
     * order_type   4 确认收货   5 超时   6 取消订单  8 删除订单
     */
    public function orderOperation(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }

        if($params['order_type'] == 8){//删除订单
            $res = Db::table('up_order')->where('order_id', $params['order_id'])->setField('order_del', 2);
        }else{
            if($params['order_type'] == 4){
                $order_suretime = date('Y-m-d H:i:s');
                Db::table('up_order')->where('order_id', $params['order_id'])->setField('order_suretime', $order_suretime);
            }
            $res = Db::table('up_order')->where('order_id', $params['order_id'])->setField('order_status', $params['order_type']);
        }
        if($res){
            if($params['order_type']==6){
                $result = Db::table('up_order')->where('order_id', $params['order_id'])->field('order_is_coupon,order_my_coupon')->find();
                if($result['order_is_coupon']==1){
                    Db::table('up_my_coupon')->where('mc_id',$result['order_my_coupon'])->setField('mc_status',1);
                }
            }
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 注册时获取年级
     * @return \think\response\Json
     */
    public function getGrade()
    {
        $grade = Db::table('up_grade')
            ->where('grade_status', 1)
            ->where('grade_del', 1)
            ->order('grade_sort')
            ->select();
        if($grade) {
            return json(['status' => 200, 'msg' => $grade]);
        } else {
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 我的收藏列表
     * @return \think\response\Json
     * page  页码
     */
    public function collectList(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $list = Db::table('up_my_like')
            ->join('up_goods', 'good_id = like_shop_id', 'left')
            ->where('like_user_id', $params['user_id'])
            ->field('like_id, good_type, good_id, good_name, good_price, good_thumb, good_y_price, 
            good_cost')
            ->order('like_time desc')
            ->page($params['page'], 10)
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除我的收藏
     * @return \think\response\Json
     * like_ids  需要删除的记录ID   格式 1,2,3
     */
    public function delCollect(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        
        $res = Db::table('up_my_like')
            ->where('like_id', 'IN', $params['like_ids'])
            ->delete();
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 客服电话
     * @return \think\response\Json
     */
    public function servicePhone(){
        $phone = Db::table('up_about')->where('id', 3)->value('content');
        if($phone){
            return json(['status' => 200, 'msg' => $phone]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 老师身份证正反面
     * @return \think\response\Json
     * user_card_front    正面
     * user_card_back     反面
     * user_zige          资格证书
     */
    public function teacherCard(){
        $params = input('param.');
        $res = $this->checkAll($params['user_id'], $params['time'], $params['value'], $params['reqKey']);
        if($res == 112) {
            return json(['status' => 301, 'msg' => $this->otherLogin]);
        }elseif($res == 110){
            return json(['status' => 302, 'msg' => $this->frost]);
        }
        $userProvince = Db::table('up_u_user')->where('user_id', $params['user_id'])->value('user_qu');
        $userInfo = Db::table('up_grade_class')->where('gs_user_id', $params['user_id'])->find();
        if(empty($userProvince) && empty($userInfo)){ //如果关联表没有教师记录并且教师没有选择地区，则不可提交
            return json(['status' => 100, 'msg' => '请先选择地区或学校组织']);
        }
        $data = [
            'user_card_front' => $params['user_card_front'], //身份证正面照
            'user_card_back' => $params['user_card_back'], //身份证反面照
            'user_zige' => $params['user_zige'], //资格证书
            'user_status' => 5, //老师需要认证
            'user_update_time' => date('Y-m-d H:i:s')
        ];
        $result = Db::table('up_u_user')->where('user_id', $params['user_id'])->update($data);
        
        if($result){
            $user_status = Db::table('up_u_user')->where('user_id', $params['user_id'])->value('user_status');
            return json(['status' => 200, 'msg' => $user_status]);
        }else{
            return json(['status' => 0, ';msg' => '上传失败']);
        }
    }

    /**
     * 我的订单&&数量
     * @param $user_id         用户ID
     * @param $order_status    订单状态    0 全部   1 待支付  2 待发货  3 待收货
     * @param $page            页码
     * @param $type            调用类型     1  列表   2 数量
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    public function order($user_id, $order_status, $page = 1, $type = 1)
    {
        $where['order_userid'] = $user_id;
        $where['order_del'] = 1;
        $where['order_after'] = ['neq', 3];
        if($order_status != 0){
            $where['order_status'] = $order_status;
        }else{
            $where['order_status'] = array('NEQ',6);
        }
        if($type == 1) {
            $res = Db::table('up_order')
                ->field('order_id, order_number, order_num, order_user_pay, order_status')
                ->where($where)
                ->order('order_time desc')
                ->page($page, 10)
                ->select();
        } else {
            $res = Db::table('up_order')->where($where)->count();
        }
        return $res;
    }
    
    /**
     * 售后列表&数量
     * @param $user_id    用户ID
     * @param int $page   分页
     * @param int $type   调用类型   1 列表  2 统计数量
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    public function afterSale($user_id, $page = 1, $type = 1){
        $where['oi_user_id'] = $user_id; //我的
        $where['oi_after_status'] = ['NEQ', 1]; //状态为申请售后的
        if($type == 1){
            $res = Db::table('up_order_info')
                ->field('oi_price, oi_num, oi_good_name, oi_good_thumb, oi_type, after_num, oi_id, 
                after_status, oi_after_status, oi_is_rerturn,oi_order_id')
                ->where($where)
                ->order('after_time desc')
                ->page($page, 10)
                ->select();
        }else{
            $where['after_status'] = ['IN', '1,2,3,4'];
            $res = Db::table('up_order_info')->where($where)->count();
        }
        return $res;
    }
    
    /**
     * 获取我的优惠券列表/数量
     * @param $user_id
     * @param $status 1 未使用  2 已使用  3 已过期
     * @param $type 1 列表  2 数量
     * @param $page
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    public function couponList($user_id, $status, $type, $page = 1)
    {
        if($type == 1) {//获取列表，带分页，用于展示
            $res = Db::table('up_my_coupon')
                ->join('up_coupon', 'mc_coupon_id = coupon_id', 'left')
                ->field('coupon_name, coupon_price, coupon_start, coupon_end, 
                coupon_thumb, mc_id, coupon_good_id, coupon_content, coupon_use, coupon_id')
                ->where('mc_user_id', $user_id)
                ->where('mc_status', $status)
                ->where('mc_type', 1)//未从我的优惠券表删除
                ->where('coupon_online', 1)//未从优惠券表删除或下架
                ->order('mc_time desc')
                ->page($page, 10)
                ->select();
        } else {//不带分页，用于统计数量
            $res = Db::table('up_my_coupon')
                ->join('up_coupon', 'mc_coupon_id = coupon_id', 'left')
                ->where('mc_user_id', $user_id)
                ->where('mc_status', $status)
                ->where('mc_type', 1)//未从我的优惠券表删除
                ->where('coupon_online', 1)//未从优惠券表删除或下架
                ->count();
        }
        return $res;
    }
    
    /**
     * 省市区字符串
     * @param $sheng
     * @param $shi
     * @param $qu
     * @return string
     */
    public function areaStr($sheng, $shi, $qu)
    {
        $shengName = Db::name('area')->where('district_id', $sheng)->value('district');
        $shiName = Db::name('area')->where('district_id', $shi)->value('district');
        $quName = Db::name('area')->where('district_id', $qu)->value('district');
        $str = $shengName . ' ' . $shiName . ' ' . $quName;
        return $str;
    }
    
    /**
     * 将半小时前的订单状态改为超时
     * @param $user_id   用户ID
     * @param $type   适用范围    1 进入订单列表调用   2 进入订单详情调用
     * @param $order_id   进入详情时的订单ID
     */
    public function timeOverOrder($user_id, $type, $order_id = ''){
        if($type == 1){//列表
            $where['order_userid'] = $user_id;//我的
            $time = date('Y-m-d H:i:s', strtotime('-30 minute'));
            $where['order_time'] = ['<=', $time];//下单时间为半个小时之前的
            $where['order_del'] = 1;
            $where['order_status'] = 1;//未支付
            $overOrder_ids = Db::table('up_order')
                ->where($where)
                ->column('order_id');
            Db::table('up_order')->where('order_id', 'IN', $overOrder_ids)->setField('order_status', 5);
            Db::table('up_order')->where('order_id', 'IN', $overOrder_ids)->setField('order_timeout_coupon', 1);
        }else{
            $order_time = Db::table('up_order')->where('order_id', $order_id)->value('order_time');
            $time = strtotime('-30 minute');//30分钟前时间戳
            $time1 = strtotime($order_time);//下单时的时间戳
            if($time1 < $time){
                $res = Db::table('up_order')->where('order_id', $order_id)->setField('order_status', 5);
                Db::table('up_order')->where('order_id', $order_id)->setField('order_timeout_coupon', 1);
                if($res){
                    return json(['status' => 200, 'msg' => '']);
                }else{
                    return json(['status' => 100, 'msg' => '']);
                }
            }
            return json(['status' => 300, 'msg' => '']);
        }
    }
    
    /**
     * 超过15天没有确认收货，则系统自动确认收货
     * @param $user_id   用户ID
     * @param $type   适用范围    1 进入订单列表调用   2 进入订单详情调用
     * @param $order_id   进入详情时的订单ID
     */
    public function autoVerifyOrder($user_id, $type, $order_id = ''){
        if($type == 1){//列表
            $where['order_userid'] = $user_id;//我的
            $time = date('Y-m-d H:i:s', strtotime('-15 days'));
            $where['order_sendtime'] = ['<=', $time];//发货时间为15天之前的
            $where['order_del'] = 1;
            $where['order_status'] = 3;//待收货
            $verifyOrder_ids = Db::table('up_order')
                ->where($where)
                ->column('order_id');
            Db::table('up_order')->where('order_id', 'IN', $verifyOrder_ids)->setField('order_status', 4);
        }else{
            $send_time = Db::table('up_order')->where('order_id', $order_id)->value('order_sendtime');
            $time = strtotime('-15 days');//15天前时间戳
            $time1 = strtotime($send_time);//发货时的时间戳
            if($time1 < $time){
                $res = Db::table('up_order')->where('order_id', $order_id)->setField('order_status', 4);
                if($res){
                    return json(['status' => 200, 'msg' => '']);
                }else{
                    return json(['status' => 100, 'msg' => '']);
                }
            }
            return json(['status' => 300, 'msg' => '']);
        }
    }
    
    /**
     * 进入优惠券列表时，改变优惠券状态----已放置base类
     * @param $user_id
     */
    public function timeOverCoupon($user_id){
        $time = date('Y-m-d H:i:s');
        //未使用、上架中的优惠券列表
        $coupon = Db::table('up_my_coupon')
            ->join('up_coupon', 'mc_coupon_id = coupon_id', 'left')
            ->where('coupon_end', '<', $time)
            ->where('coupon_status', 1)
            ->where('mc_status', 1)
            ->where('coupon_online', 1)
            ->field('coupon_id')
            ->where('mc_user_id', $user_id)
            ->select();
        foreach($coupon as $k => $v) {
            //修改优惠券状态
            Db::table('up_coupon')->where('coupon_id', $v['coupon_id'])->setField('coupon_status', 2);
            //修改我的优惠券状态
            Db::table('up_my_coupon')
                ->where('mc_status', 1)
                ->where('mc_type', 1)
                ->where('mc_coupon_id', $v['coupon_id'])
                ->setField('mc_status', 3);
        }
    }
}